#include <hardware/ir/ir.h>
#include <IRremote.h>
//红外编码为NEC
#define DECODE_NEC
//通过红外设置舵机转动角度或停留时间：22-转动角度， 13-停留时间，默认是转动角度
int flag = 22;
int runningFlag = 0;
void initIR()
{
    // 初始化红外遥控
    IrReceiver.begin(RECV_PIN, DISABLE_LED_FEEDBACK);
}
void scanReceive()
{
    if (IrReceiver.decode())
    {
        int value = IrReceiver.decodedIRData.decodedRawData;
        if(value == 0){
            // 接收下一个值
            IrReceiver.resume(); 
            return;           
        }
        uint16_t address = IrReceiver.decodedIRData.address;
        uint16_t command = IrReceiver.decodedIRData.command;
        //* => 22
        if(address == 0 && command == 22)
        {
            flag = command;
        }//# => 13
        else if(address == 0 && command == 13){
            flag = command;
        }
        if(flag == 22){
            if(address == 0 && command == 69){
                configData.servoAngle = 10;
                saveConfigData();
            }
            else if(address == 0 && command == 70)
            {
                configData.servoAngle = 20;
                saveConfigData();
            }
            else if(address == 0 && command == 71)
            {
                configData.servoAngle = 30;
                saveConfigData();
            }
            else if(address == 0 && command == 68)
            {
                configData.servoAngle = 40;
                saveConfigData();
            }
            else if(address == 0 && command == 64)
            {
                configData.servoAngle = 50;
                saveConfigData();
            }
            else if(address == 0 && command == 67)
            {
                configData.servoAngle = 60;
                saveConfigData();
            }
            else if(address == 0 && command == 7)
            {
                configData.servoAngle = 70;
                saveConfigData();
            }   
            else if(address == 0 && command == 21)
            {
                configData.servoAngle = 80;
                saveConfigData();
            }  
            else if(address == 0 && command == 9)
            {
                configData.servoAngle = 90;
                saveConfigData();
            }             
        }
        if(flag == 13){
            if(address == 0 && command == 69){
                configData.servoStopTime = 100;
                saveConfigData();
            }
            else if(address == 0 && command == 70)
            {
                configData.servoStopTime = 200;
                saveConfigData();
            }
            else if(address == 0 && command == 71)
            {
                configData.servoStopTime = 300;
                saveConfigData();
            }
            else if(address == 0 && command == 68)
            {
                configData.servoStopTime = 400;
                saveConfigData();
            }
            else if(address == 0 && command == 64)
            {
                configData.servoStopTime = 500;
                saveConfigData();
            }
            else if(address == 0 && command == 67)
            {
                configData.servoStopTime = 600;
                saveConfigData();
            }
            else if(address == 0 && command == 7)
            {
                configData.servoStopTime = 700;
                saveConfigData();
            }   
            else if(address == 0 && command == 21)
            {
                configData.servoStopTime = 800;
                saveConfigData();
            }  
            else if(address == 0 && command == 9)
            {
                configData.servoStopTime = 900;
                saveConfigData();
            }             
        }        
        if(address == 0 && command == 28)
        {
            if(runningFlag == 0)
            {
                runningFlag = 1;
                pushButton();
                runningFlag = 0;
            }
        }
        // 接收下一个值
        IrReceiver.resume();
    }
}
